//
//  AHUViewController.m
//  Qihuo
//
//  Created by Cheng Qi on 13-12-10.
//  Copyright (c) 2013年 Vencoo. All rights reserved.
//

#import "AHUViewController.h"

@interface AHUViewController ()

@end

@implementation AHUViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)dealloc
{
    [_downText release];
    [_upText release];
    [_pickerBackView release];
    [_contentView release];
    [super dealloc];
}
#pragma mark---Action
-(void)btnClick:(UIButton*)send
{
    _pickerBackView.hidden=NO;
}

#pragma mark---Resource
-(void)addPickerTimeView
{
    _pickerBackView = [[PickerBackgroundView alloc]initWithFrame:CGRectMake(0, 0, scrennWidth, screenHeight)];
    _pickerBackView.userInteractionEnabled = YES;
    _pickerBackView.butonDelegate = self;
    [_pickerBackView canDate];
    _pickerBackView.tag = 0;
    [self.view addSubview:_pickerBackView];
    _pickerBackView.hidden = YES;
}
-(void)changeView
{
    UILabel *weatherLabel =[[UILabel alloc]initWithFrame:CGRectMake(10,74,150, 50)];
    weatherLabel.text=@"设备编号:";
    [weatherLabel setTextColor:[UIColor whiteColor]];
    weatherLabel.backgroundColor =[UIColor clearColor];
    [weatherLabel setFont:[UIFont systemFontOfSize:30]];
    UITextField *weatherText =[[UITextField alloc]initWithFrame:CGRectMake(170,74, 300, 50)];
    [weatherText setTextColor:[UIColor whiteColor]];
    weatherText.delegate =self;
    weatherText.tag =66;
    [weatherText setBorderStyle:UITextBorderStyleLine];
    [self.view addSubview:weatherLabel];
    [weatherLabel release];
    [self.view addSubview:weatherText];
    [weatherText release];
    
    UILabel *temperLabel =[[UILabel alloc]initWithFrame:CGRectMake(520,74,100, 50)];
    [temperLabel setTextColor:[UIColor whiteColor]];
    [temperLabel setTextAlignment:NSTextAlignmentCenter];
    temperLabel.text=@"日期:";
    [temperLabel setFont:[UIFont systemFontOfSize:30]];
    UITextField *temperText =[[UITextField alloc]initWithFrame:CGRectMake(620,74, 200, 50)];
    temperText.tag=9999;
    temperText.enabled = NO;
    NSDateFormatter *fom = [NSDateFormatter new];
    [fom setDateFormat:@"yyyy年MM月dd日"];
    temperText.text = [fom stringFromDate:[NSDate date]];
    temperText.textColor = [UIColor whiteColor];
    [temperText setFont:[UIFont systemFontOfSize:25]];
    [temperText setBorderStyle:UITextBorderStyleLine];
    temperLabel.backgroundColor =[UIColor clearColor];
    [self.view addSubview:temperLabel];
    [temperLabel release];
    [self.view addSubview:temperText];
    [temperText release];
    UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];//日历
    [btn setBackgroundImage:[UIImage imageNamed:@"日历按钮.png"] forState:UIControlStateNormal];
    btn.frame =CGRectMake(880, 61, 80, 72);
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
   
    UIImage *blackImage = [UIImage imageNamed:@"黑底"];
    self.myTableView.frame=CGRectMake(interval111/2, 128, 0,screenHeight - blackImage.size.height - 128);
    self.whiteImageView.frame=CGRectMake(self.myTableView.frame.origin.x + self.myTableView.frame.size.width, self.myTableView.frame.origin.y, scrennWidth - (self.myTableView.frame.origin.x)*2 -self.myTableView.frame.size.width, self.myTableView.frame.size.height);
    self.backScrollV.frame =CGRectMake(10, 10, self.whiteImageView.frame.size.width - 20, self.whiteImageView.frame.size.height - 20);
    NSLog(@"width:%f--height:%f",self.backScrollV.frame.size.width,self.backScrollV.frame.size.height);
    _contentView =[[[NSBundle mainBundle]loadNibNamed:@"AhuView" owner:self options:nil]lastObject];
    [self.backScrollV addSubview:_contentView];
    UIButton *upBtn =(UIButton*)[_contentView viewWithTag:1];
    [upBtn addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
    upBtn.tag=1;
    UIButton *downBtn =(UIButton*)[_contentView viewWithTag:2];
    [downBtn addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
    downBtn.tag=2;
    _upText =(UITextView*)[_contentView viewWithTag:123];
    _downText=(UITextView*)[_contentView viewWithTag:321];
    
}
-(void)btn:(UIButton*)send
{
    switch (send.tag) {
        case 1:
        {
            if (_upBtnTouched==NO) {
                [send setImage:[UIImage imageNamed:@"选中.png"] forState:UIControlStateNormal];
                _upBtnTouched=YES;
                [_upText becomeFirstResponder];
            }else
            {
                [send setImage:[UIImage imageNamed:@"未选中.png"] forState:UIControlStateNormal];
                _upBtnTouched=NO;
                [_upText resignFirstResponder];
            }
        }
            break;
        case 2:
        {
            if (_downBtnTouched==NO) {
                [send setImage:[UIImage imageNamed:@"选中.png"] forState:UIControlStateNormal];
                _downBtnTouched=YES;
                [_downText becomeFirstResponder];
            }else
            {
                [send setImage:[UIImage imageNamed:@"未选中.png"] forState:UIControlStateNormal];
                _downBtnTouched=NO;
                [_downText resignFirstResponder];
            }
        }
            break;
        default:
            break;
    }
   
}
-(void)jajajajaj:(UIButton *)btn
{
    switch (btn.tag) {
        case 0:
        {
            [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            NSMutableDictionary *upDic =[[NSMutableDictionary alloc]initWithCapacity:0];
            if (_upBtnTouched==NO) {
                [upDic setObject:@"0" forKey:@"inspection"];
                [upDic setObject:@"" forKey:@"inContent"];
            }else{
                [upDic setObject:@"1" forKey:@"inspection"];
                [upDic setObject:[_upText.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]forKey:@"inContent"];
            }
            if (_downBtnTouched==NO) {
                [upDic setObject:@"0" forKey:@"maintenance"];
                [upDic setObject:@"" forKey:@"maContent"];
            }else{
                [upDic setObject:@"1" forKey:@"maintenance"];
                [upDic setObject:[_downText.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:@"maContent"];
            }
            NSArray *arr =[[NSArray alloc]initWithObjects:upDic,nil];
            [upDic release];
            [_sendDic setObject:arr forKey:@"checkData"];
            [_sendDic setObject:[[Appinfomation shareIstance].loginDic valueForKey:@"id"] forKey:@"userid"];
            UITextField *remarkTv = (UITextField *)[self.blackStopV viewWithTag:290];
            [_sendDic setObject:[self.checkArea stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:@"checkArea"];
            [_sendDic setObject:remarkTv.text forKey:@"abolishRemark"];
            [_sendDic setObject:@"0" forKey:@"isAbolished"];

            [arr release];
            NSLog(@"%@",_sendDic);
            NSString *sendStr =[_sendDic JSONRepresentation];
            ASIFormDataRequest *request =[ASIFormDataRequest requestWithURL:[NSURL URLWithString:addAHU]];
            request.delegate =self;
            [request setRequestMethod:@"POST"];
            [request setPostValue:sendStr forKey:@"data"];
            [request  startAsynchronous];
        }
            break;
            
        default:
            break;
    }
}
#pragma mark--ASIHttpRequest
-(void)requestFinished:(ASIHTTPRequest *)request
{
    [self showAlertSuccess];
}
-(void)requestFailed:(ASIHTTPRequest *)request
{
    [self showAlertFail];
}
#pragma mark---UITextField

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField.tag==66) {
        [_sendDic setObject:textField.text forKey:@"device"];
    }
}
#pragma mark - buttonDelegate
- (void)sureOrCancelDelegate:(UIButton*)btn tag:(NSInteger)tag
{
    UITextField *temperText =(UITextField*)[self.view viewWithTag:9999];
    if (btn.tag == 0) {
        if (_pickerBackView.timeStr.length) {
            temperText.text = _pickerBackView.timeStr ;
        }else{
            NSDateFormatter *fom = [NSDateFormatter new];
            [fom setDateFormat:@"yyyy年MM月dd日"];
            temperText.text = [fom stringFromDate:[NSDate date]];
        }
    }
    [_sendDic setObject:[temperText.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:@"checkTime"];
    [_sendDic setObject:[[Appinfomation shareIstance].loginDic valueForKey:@"id"] forKey:@"userId"];
    self.navigationController.navigationBar.hidden = 0;
    _pickerBackView.hidden = YES;
}
-(void)addArrar
{
    _sendDic =[[NSMutableDictionary alloc]initWithCapacity:0];
}

- (void)viewDidLoad
{
    self.title =@"AHU/机房巡检计划/保养记录";
    [super viewDidLoad];
    [self addArrar];
    _upBtnTouched =NO;
    _downBtnTouched =NO;
    UIButton *btn =(UIButton*)[ self.blackView viewWithTag:0];
    [btn setImage:[UIImage imageNamed:@"提交.png"] forState:UIControlStateNormal];
	// Do any additional setup after loading the view.
    [self changeView];
    [self addPickerTimeView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
